﻿using PosClient.Helper;
using PosClient.Model;
using PosClient.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PosClient.View.Report
{
    public partial class ReportView : UserControl
    {
        public ReportView()
        {
            InitializeComponent();
            GetData();
            DrawDrstring();
        }


        public OrderReport reportData { get; set; } = new OrderReport()
        {
            FirstTradeTime = Convert.ToDateTime("1999-12-31 00:00:00"),
            LastTradeTime = Convert.ToDateTime("1999-12-31 23:59:59")
        };

        public List<OrderPayReport> orderPayReport { get; set; }

        private void GetData()
        {
            using (var conn = FreeClient.Sqlite)
            {
                using (var trans = conn.CreateUnitOfWork())
                {

                    try
                    {
                        Pos pos = conn.Select<Pos>().Where(x => x.Status == 1).First();
                        if (pos.BalanceDate == null)
                        {
                            pos.BalanceDate = DateTime.Now;
                        }
                        string reportDate = pos.BalanceDate.ToString("yyyy-MM-dd");

                        var order = conn.Select<Order>().Where(x => x.CreateTime.ToString("yyyy-MM-dd") == reportDate).ToList();
                        if (order.Count > 0)
                        {
                            reportData.FirstTradeTime = order.Min(x => x.CreateTime);
                            reportData.LastTradeTime = order.Max(x => x.CreateTime);
                            reportData.TotalAmount = order.Sum(x => x.Amount);
                            reportData.TotalCount = order.Count;
                            reportData.TotalSaleCount = order.Where(x => x.FlagRefund == 0).ToList().Count;
                            reportData.TotalRefundCount = order.Where(x => x.FlagRefund > 0).ToList().Count;

                            reportData.TotalSaleAmount = order.Where(x => x.FlagRefund == 0).ToList().Sum(y => y.Amount);
                            reportData.TotalRefundAmount = order.Where(x => x.FlagRefund > 0).ToList().Sum(y => y.Amount);

                            var pay = conn.Select<OrderPay>().Where(x => x.CreateTime.ToString("yyyy-MM-dd") == reportDate).ToList();

                            var pays = pay.GroupBy(g => g.PayName).Select(x => new OrderPayReport() { PayName = x.Key, PayAmount = x.Sum(p => p.PayAmount) });
                            orderPayReport = pays.ToList();
                        }
                    }
                    catch (Exception err)
                    {
                        Logger.Default.Error(err.Message, err);
                        trans.Rollback();
                    }
                    trans.Commit();
                }
            }
        }

        private void DrawDrstring()
        {
            if (reportData != null)
            {
                var size = new Size();
                // 准备数据

                // 创建位图
                pictureBox1.Image = null;
                if (pictureBox1.Width > 0)
                {
                    Bitmap bmp = new Bitmap(pictureBox1.Width, 3600);
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        g.Clear(Color.White);
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

                        //计算副标题字体的大小
                        var subTitleSize = g.MeasureString("收 银 机 销 售 报 告", new Font("微软雅黑", 13));

                        var subTitleX = (pictureBox1.Width - subTitleSize.Width) / 2;
                        var titleY = 1 + 20 / 2;
                        Pen borderPen = new Pen(Color.Black, 0.5f);
                        Font foreFont = new Font("微软雅黑", 13, FontStyle.Bold);
                        Font headerFont = new Font("Microsoft YaHei", 9);

                        //绘制标题
                        using (var brush = new SolidBrush(Color.Black))
                        {
                            g.DrawString("收 银 机 销 售 报 告", new Font("微软雅黑", 13, FontStyle.Bold), brush, subTitleX, titleY);

                            titleY += 30;

                            g.DrawLine(borderPen, 60, titleY, pictureBox1.Width - 50, titleY);

                            titleY += 15;

                            g.DrawString($"首次交易时间", headerFont, brush, 60, titleY);

                            g.DrawString($"最后交易时间", headerFont, brush, 380, titleY);

                            titleY += 15;

                            g.DrawString($"{reportData.FirstTradeTime}", foreFont, brush, 60, titleY);

                            g.DrawString($"{reportData.LastTradeTime}", foreFont, brush, 380, titleY);

                            titleY += 40;

                            g.DrawString("总额(元)", headerFont, brush, 60, titleY);

                            g.DrawString("总单量(笔)", headerFont, brush, 200, titleY);

                            g.DrawString("总销售额(元)", headerFont, brush, 340, titleY);

                            g.DrawString("总退货额(元)", headerFont, brush, 480, titleY);

                            titleY += 15;

                            g.DrawString($"{reportData.TotalAmount}", foreFont, brush, 60, titleY);

                            g.DrawString($"{reportData.TotalCount}", foreFont, brush, 200, titleY);

                            g.DrawString($"{reportData.TotalSaleAmount}", foreFont, brush, 340, titleY);

                            g.DrawString($"{reportData.TotalRefundAmount}", foreFont, brush, 480, titleY);

                            titleY += 40;

                            g.DrawString($"销售笔数", headerFont, brush, 60, titleY);

                            g.DrawString($"退货笔数", headerFont, brush, 480, titleY);

                            titleY += 15;

                            g.DrawString($"{reportData.TotalCount}", foreFont, brush, 60, titleY);

                            g.DrawString($"{reportData.TotalRefundCount}", foreFont, brush, 480, titleY);

                            titleY += 40;

                            g.DrawString($"支付方式列表", foreFont, brush, 60, titleY);

                            titleY += 25;

                            g.DrawLine(borderPen, 60, titleY, 460, titleY);
                            titleY += 15;

                            if (orderPayReport != null && orderPayReport.Count > 0)
                            {
                                foreach (var pay in orderPayReport)
                                {
                                    g.DrawString($"{pay.PayName}", foreFont, brush, 60, titleY);

                                    g.DrawString($"{pay.PayAmount}", foreFont, brush, 200, titleY);

                                    titleY += 25;
                                }
                            }

                            titleY += 125;
                        }
                        // 更新PictureBox
                        if (pictureBox1.Image != null)
                            pictureBox1.Image.Dispose();
                        pictureBox1.Image = bmp;
                        //设置图像控件长度
                        pictureBox1.Height = size.Height + 50;
                    }
                }
            }
        }

    }
}
